<template>
	<view class="">
		<view class="main_bg">
			<view v-if="goodsList.length>0" class="back_mag">
				<view v-for="(item,index) in goodsList" :key="index" class="item_li" @click="goDetail(item.id)">
					<view class="top_brand_type">
						<span class="tag" v-if="!item.overInsured">在保</span>
						<span class="tag tag_p" v-else>过保</span>
						<span class="day_color" v-if="!item.overInsured">{{item.warrantyDay}}天</span>
					</view>
					<view class="conent">
						<view class="left_img">
							<image v-if="item.image" @click="lookImg(item.image)" class="img" lazy-load
								:src="item.image" mode="aspectFill"></image>
							<uni-icons color="#aac" size="48" v-else type="image"></uni-icons>
						</view>
						<view class="right_text">
							<view class="brands">
								{{'【'+item.goodsBrand+'】'+item.goodsName}}
							</view>
							<view class="pt"><span class="tip">一物一码：</span>{{item.code}}</view>
							<view class="pt"><span class="tip">保修期至：</span>{{item.warrantyEndTime}}</view>
						</view>
					</view>
				</view>
				<view class="no_some" v-if="title">
					{{title}}
				</view>
			</view>
			<view v-else class="void_goods">
				<image class="img" src="/static/goods.svg"></image>
				<view class="tip">暂无商品</view>
			</view>
		</view>
		<view class="btn_logout">
			<button type="primary" size="small" @click="addOpen" class="onlogout">
				<span>添加商品</span>
			</button>
		</view>
		<!-- 添加商品 弹窗 -->
		<uni-popup ref="popup" type="center">
			<view class="add_main">
				<p>添加商品</p><uni-icons class="close_this" size="20" @click="popup.close()" color="#999"
					type="closeempty"></uni-icons>
				<uni-easyinput suffixIcon="scan" placeholderStyle="font-size:28rpx;" v-model="code"
					placeholder="请输入或扫描机身编码" @iconClick="iconClick"></uni-easyinput>
				<button class="onlogout" @click="addCode"><span>添加</span></button>
			</view>
		</uni-popup>
	</view>
</template>

<script setup lang="ts">
	import {
		ref, onMounted
	} from "vue";
	import { onReachBottom, onPullDownRefresh,onUnload } from '@dcloudio/uni-app'
	import apiGoods from '@/util/api/goods.js'
	import apiCode from '@/util/api/code.js'
	const goodsList = ref([])
	const total = ref(0)
	const page = ref(1)
	const popup = ref()
	const code = ref('')
	const title = ref('没有更多工单')
	const addOpen = () => {
		//打开弹窗
		popup.value.open()
	}
	onMounted(() => {
		getList()
	})
	onPullDownRefresh(async () => {
		goodsList.value = []
		page.value = 1
		code.value = ''
		await getList()
		uni.stopPullDownRefresh(); // 停止下拉刷新状态
	})
	onUnload(() => {
		uni.hideLoading()
	})
	// 获取商品列表
	const getList = async () => {
		let data = {
			"limit": 0,
			"page": page.value
		}
		uni.showLoading({
			title: '加载中...'
		})
		await apiGoods.getGoodsList(data).then((res : { code : number; data : any; }) => {
			if (res.code === 20000) {
				goodsList.value = [...goodsList.value, ...res.data.rows]
				total.value = res.data.total
			}
		})
		uni.hideLoading()
	}
	// 商品详情
	const goDetail = (id : string) => {
		uni.navigateTo({
			url: '/pages/goods/detail?id=' + id
		})
	}
	// 预览图片
	const lookImg = (url) => {
		uni.previewImage({
			urls: [url],
		})
	}
	// 调起客户端扫码界面，扫码成功后返回对应的结果。
	const iconClick = () => {
		uni.scanCode({
			scanType: ['qrCode', 'barCode'],//扫码类型
			success: async function (res) {
				code.value = res.result
				await addCode()
			},
			fail: (err) => {
				// #ifdef H5
				uni.showToast({
					icon: 'none',
					title: '设备不支持此功能',
					duration: 3000
				})
				// #endif
			}
		})
	}
	// 添加编码
	const addCode = async () => {
		if (code.value) {
			uni.showLoading({
				title: "加载中...",
			})
			await apiCode.getRegisterSecurityCheck({ code: code.value }).then(res => {
				if (res.code == 20000) {
					// 判断是否登记激活
					if (!res.data.row.register) {
						popup.value.close()
						uni.redirectTo({
							url: '/pages/card/acitvation?id=' + res.data.row.id
						})
					} else {
						uni.showToast({
							title: '该商品已登记激活！',
							icon: 'none'
						})
					}
				}
			})
			uni.hideLoading()
		} else {
			uni.showToast({
				title: '请先输入编码',
				icon: 'none'
			})
		}
	}
	// 触底加载更多
	onReachBottom(() => {
		if (total.value > goodsList.value.length) {
			page.value = page.value + 1
			title.value = ''
			getList()
		} else {
			title.value = '没有更多工单'
		}
	})
</script>
<style lang="scss" scoped>
	.main_bg {
		height: 100vh;
		background-color: #f6f7fa;

		.back_mag {
			padding-bottom: 200rpx;
		}

		.void_goods {
			display: flex;
			justify-content: center;
			align-items: center;
			flex-direction: column;
			width: 100%;
			height: 60vh;

			.img {
				width: 60%;
				height: 250rpx;
			}

			.tip {
				color: #aaa;
				font-size: 28rpx;
			}
		}

		.no_some {
			font-size: 24rpx;
			color: #ccc;
			text-align: center;
			width: 100%;
		}

		.item_li {
			width: calc(100% - 48rpx);
			display: flex;
			justify-content: center;
			flex-direction: column;
			align-items: center;
			background-color: #fff;
			margin: 24rpx;
			border-radius: 10rpx;
			box-shadow: 0 0 20rpx 10rpx #f0f0f0;
			position: relative;
			padding: 20rpx 0;

			.top_brand_type {
				position: relative;
				text-align: right;
				right: 12rpx;
				top: -6rpx;
				width: 100%;
				.tag {
					background-color: #007cff;
					color: #fff;
					font-size: 24rpx;
			
					border-radius: 4rpx;
					margin-right: 6rpx;
					padding:2rpx 4rpx;
					&.tag_p {
						background-color: #ccc;
						color: #eee;
					}
				}

				.day_color {
					background-color: #007cff;
					color: #fff;
					font-size: 24rpx;
					padding: 2rpx 4rpx;
					border-radius: 4rpx;
				}
			}

			.conent {
				display: flex;
				align-items: center;
				width: 100%;

				.left_img {
					margin: 0 20rpx;
					width: 180rpx;
					height: 180rpx;
					background-color: #fff;
					border-radius: 10rpx;
					border: 1px solid #eee;

					.img {
						width: 100%;
						height: 100%;
						border-radius: 10rpx;
					}
				}

				.right_text {
					font-size: 24rpx;
					margin-right: 12rpx;
					flex: 1;

					.brands {
						display: -webkit-box;
						-webkit-line-clamp: 2;
						-webkit-box-orient: vertical;
						overflow: hidden;
						word-wrap: break-word;
						word-break: break-all;
						font-weight: bold;
						font-size: 28rpx;
						margin: 0rpx 0 12rpx;
					}


					.goods_ms {
						display: flex;
						justify-content: space-between;
						align-items: center;
						width: 100%;

						.ms {
							display: flex;
							justify-content: center;
							flex-direction: column;
							align-items: center;
							width: 100%;
							word-wrap: break-word;
							word-break: break-all;
							margin: 8rpx;

							.tip {
								color: #777;
							}
						}

						.line {
							height: 50rpx;
							width: 2rpx;
							background-color: #ccc;
						}
					}


					.pt {
						margin-top: 10rpx;

						.tip {
							color: #777;
						}
					}
				}
			}
		}

	}

	.add_main {
		background-color: #fff;
		border-radius: 20rpx;
		width: 80vw;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		padding: 24rpx;
		position: relative;

		.close_this {
			position: absolute;
			right: 24rpx;
			top: 24rpx;
		}

		.onlogout {
			font-size: 28rpx;
			margin: 30rpx 20rpx 0;
			height: 80rpx;
			border-radius: 50rpx;
			line-height: 80rpx;
			width: 100%;
			color: #fff;
			background-color: #007cff;
		}

		:deep(.is-input-border) {
			// border-radius: 50rpx !important;
			background-color: rgba(243, 244, 248, 1) !important;
			padding: 10rpx;
			margin-top: 20rpx;
			border: none;
			height: 80rpx !important;
			line-height: 80rpx;
			font-size: 28rpx
		}
	}

	.btn_logout {
		position: fixed;
		bottom: 0rpx;
		margin: 0 auto;
		width: 100%;
		z-index: 3;
		background-color: #fff;
		padding: 20rpx 0;

		:deep(button:after) {
			border: none !important;
		}

		.onlogout {
			font-size: 28rpx;
			padding: 0rpx 20rpx;
			margin: 0 200rpx;
			height: 80rpx;
			border-radius: 50rpx;
			line-height: 80rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			background-color: #007cff;
		}
	}
</style>